﻿#include "CommonSetForm.h"
#include "ui_CommonSetForm.h"
#include "iconhelper.h"
#include "myapp.h"
#include "mylabel.h"
#include "singleton.h"
#include "myapp.h"

#include <QMessageBox>

CommonSetForm::CommonSetForm(QWidget* parent)
    : QWidget(parent)
    , ui(new Ui::CommonSetForm)
    , first(true)
{
    ui->setupUi(this);
    this->initForm();
}

CommonSetForm::~CommonSetForm()
{
    delete ui;
}
void CommonSetForm::initForm()
{

    QPixmap syncTime = IconHelper::Instance()->getPixmap("#DCDCDC", 0xf110, 10, 8, 8);
    QPixmap rebootPro = IconHelper::Instance()->getPixmap("#DCDCDC", 0xf021, 10, 8, 8);
    QPixmap rebootDev = IconHelper::Instance()->getPixmap("#DCDCDC", 0xf01e, 10, 8, 8);
    ui->btn_syncTime->setIcon(QIcon(syncTime));
    ui->btn_syncTime->setIconSize(QSize(10, 10));
    ui->btn_rebootPro->setIcon(QIcon(rebootPro));
    ui->btn_rebootPro->setIconSize(QSize(10, 10));
    ui->btn_rebootDev->setIcon(QIcon(rebootDev));
    ui->btn_rebootDev->setIconSize(QSize(10, 10));

    QHBoxLayout* pSearchLayout = new QHBoxLayout(ui->led_modVer);
    pSearchLayout->addStretch();
    pSearchLayout->setSpacing(0);
    pSearchLayout->setContentsMargins(0, 0, 0, 0);
    MyLabel* detail = new MyLabel(this);
    detail->resize(ui->led_modVer->height(), ui->led_modVer->height());
    detail->setFocusPolicy(Qt::NoFocus);
    detail->setCursor(QCursor(Qt::PointingHandCursor));

    detail->setStyleSheet("background-color: rgb(9, 19, 50);color: rgb(255, 255, 255);"
                          "width:24px;height:24px;margin:2px;");
    //connect(search, &QPushButton::clicked, this, &MainWindow::btn_search_clicked);
    QPixmap pix = IconHelper::Instance()->getPixmap("#255990", 0xf075, ui->led_modVer->height() - 5, ui->led_modVer->height() - 5, ui->led_modVer->height() - 5);
    detail->setPixmap(pix);
    pSearchLayout->addWidget(detail);
    ui->led_modVer->setTextMargins(0, 0,  ui->led_modVer->height(), 0);

    QGroupBox* gpx = new QGroupBox(this);
    QVBoxLayout* vlayout = new QVBoxLayout();
    lab_info = new QLabel;
    lab_info->setText(mod_info);
    vlayout->addWidget(lab_info);
    vlayout->setMargin(0);
    gpx->setStyleSheet("QGroupBox{background-color: black;color:white;}");
    gpx->setLayout(vlayout);
    gpx->resize(231, 181);
    // gpx->move(ui->led_modVer->geometry().right() - 100, ui->led_modVer->geometry().bottom() + 10);
    gpx->hide();
    connect(detail, &MyLabel::show, this, [ = ]
    {
        gpx->show();
        gpx->move(ui->led_modVer->geometry().right() + 100, ui->led_modVer->geometry().bottom());


    });
    connect(detail, &MyLabel::hide, this, [ = ]
    {
        gpx->hide();
    });

    this->InputValidator();
}

void CommonSetForm::InputValidator()
{
    QRegExp reg("^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\."
                "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
                "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
                "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$");
    ui->lineEdit_ip0->setValidator(new QRegExpValidator(reg));
    ui->lineEdit_ip1->setValidator(new QRegExpValidator(reg));
    ui->lineEdit_mask->setValidator(new QRegExpValidator(reg));
    ui->lineEdit_gw->setValidator(new QRegExpValidator(reg));
    ui->lineEdit_dns->setValidator(new QRegExpValidator(reg));
}


void CommonSetForm::onGetDeviceInfo(QJsonObject obj)
{
    ui->led_hardware->setText(obj.value("hw").toString());
    ui->led_software->setText(obj.value("sw").toString());
    ui->led_algoVer->setText(obj.value("algoVer").toString());
    ui->led_modVer->setText(obj.value("modVer").toString());
    ui->led_fwVer->setText(obj.value("fwVer").toString());
    QJsonArray chnInfo = obj["chnInfo"].toArray();
    if(obj.value("devType").toString() == "AiCam_H")
    {
        ui->led_dev_type->setText(myApp::AiCam_H);
        myApp::current_chnnid = chnInfo.at(0).toObject().value("chnId").toString();
    }
    else if(obj.value("devType").toString() == "AiCam_L")
    {
        ui->led_dev_type->setText(myApp::AiCam_L);
        myApp::current_chnnid = chnInfo.at(0).toObject().value("chnId").toString();
    }
    else if(obj.value("devType").toString() == "Ai_ARM")
    {
        ui->led_dev_type->setText(myApp::Ai_ARM);
    }
    else if(obj.value("devType").toString() == "Ai_GPU")
    {
        ui->led_dev_type->setText(myApp::Ai_GPU);
    }
    else if(obj.value("devType").toString() == "Ai_DZFT")
    {
        ui->led_dev_type->setText(myApp::Ai_DZFT);
    }
    ui->led_sn->setText(obj.value("sn").toString());
    ui->led_dev_id->setText(obj.value("devId").toString());
    devId = obj.value("devId").toString();
    myApp::current_devId = devId;
    if(first)
    {
        this->on_btn_GetDeviceRunInfo_clicked();
        this->on_btn_getNetParam_clicked();
        first = false;
    }
}
void CommonSetForm::onGetDeviceRunInfo(QJsonObject obj)
{
    ui->label_devTime->setText(obj.value("dayTime").toString());
    ui->progressBar_cpu->setValue(obj.value("cpuUsage").toInt());
    ui->progressBar_ram->setValue(obj.value("ramUsage").toInt());
    ui->progressBar_disk->setValue(obj.value("diskUsage").toInt());
    ui->progressBar_temp->setValue(obj.value("temp").toDouble());

}
void CommonSetForm::onGetNetworkParam(QJsonObject obj)
{
    ui->lineEdit_ip0->setText(obj.value("ip1").toString());
    ui->lineEdit_ip1->setText(obj.value("ip2").toString());
    ui->lineEdit_mask->setText(obj.value("netmask").toString());
    ui->lineEdit_gw->setText(obj.value("gateway").toString());
    ui->lineEdit_dns->setText(obj.value("dns").toString());
    ui->lineEdit_mac->setText(obj.value("mac").toString());
}


void CommonSetForm::onGetAlgoInfo(QJsonObject obj)
{
    mod_info = obj.value("info").toString();
    lab_info->setText(mod_info);
}
void CommonSetForm::on_btn_getdevinfo_clicked()
{
    QJsonObject body;
    Singleton::Instance()->onSendParam("GetDeviceInfo", body);
}
void CommonSetForm::on_btn_GetDeviceRunInfo_clicked()
{
    QJsonObject body;
    body.insert("devId", devId);
    Singleton::Instance()->onSendParam("GetDeviceRunInfo", body);
    Singleton::Instance()->onSendParam("GetAlgoInfo", body);
}


void CommonSetForm::on_btn_getNetParam_clicked()
{
    QJsonObject body;
    body.insert("devId", devId);
    Singleton::Instance()->onSendParam("GetNetworkParam", body);
}

void CommonSetForm::on_btn_setNetwork_clicked()
{
    if(ui->lineEdit_ip0->text().isEmpty() || ui->lineEdit_mask->text().isEmpty() || ui->lineEdit_gw->text().isEmpty() || ui->lineEdit_dns->text().isEmpty())
    {
        if(QMessageBox::warning(this, tr("waring"), tr("no param"), QMessageBox::Ok, QMessageBox::NoButton))
        {
            return;
        }
    }
    QJsonObject body;
    body.insert("devId", devId);
    body.insert("ip1", ui->lineEdit_ip0->text());
    body.insert("ip2", ui->lineEdit_ip1->text());
    body.insert("netmask", ui->lineEdit_mask->text());
    body.insert("gateway", ui->lineEdit_gw->text());
    body.insert("dns", ui->lineEdit_dns->text());
    Singleton::Instance()->onSendParam("SetNetworkParam", body);
}

void CommonSetForm::on_btn_syncTime_clicked()
{
    QJsonObject body;
    body.insert("devId", devId);
    body.insert("year", QDateTime::currentDateTime().date().year());
    body.insert("mon", QDateTime::currentDateTime().date().month());
    body.insert("day", QDateTime::currentDateTime().date().day());
    body.insert("hour", QDateTime::currentDateTime().time().hour());
    body.insert("min", QDateTime::currentDateTime().time().minute());
    body.insert("sec", QDateTime::currentDateTime().time().second());
    Singleton::Instance()->onSendParam("SyncDateTime", body);
}



void CommonSetForm::on_btn_rebootPro_clicked()
{
    QJsonObject body;
    body.insert("devId", devId);
    QMessageBox m_box(QMessageBox::Question, QString("请求"), QString("是否重启程序"), QMessageBox::Ok | QMessageBox::Cancel);
    if(m_box.exec() == QMessageBox::Ok)
    {

        Singleton::Instance()->onSendParam("Restart", body);
    }

}

void CommonSetForm::on_btn_rebootDev_clicked()
{
    QJsonObject body;
    body.insert("devId", devId);
    QMessageBox m_box(QMessageBox::Question, QString("请求"), QString("是否重启系统"), QMessageBox::Ok | QMessageBox::Cancel);
    if(m_box.exec() == QMessageBox::Ok)
    {
        Singleton::Instance()->onSendParam("Reboot", body);
    }

}
